/*
 * Created on Jun 4, 2007
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package it.unibo.deis.lia.mmhc.mpe;

import java.util.Vector;
import it.unibo.deis.lia.location.LocationOss;

public class MobilityEstimator {
	private MobilityEstimatorIEEE80211 mobilityEstimatorIEEE80211 = null;
	private MobilityEstimatorBluetooth mobilityEstimatorBluetooth = null;

	private int currentPhase = MobilityEstimator.SEARCH;
	public static final int SEARCH = 0;
	public static final int CONNECTED = 1;

	public static final int NOT_AVAILABLE = -1;
	public static final int STILL = 0;
	public static final int MOTION = 1;

	/*
	 * public static final int StandardDevice=0; public static final int
	 * SIMULATED=-1; public static final int UNUSED=-2;
	 */

	private static MobilityEstimator mobilityEstimatorInstance = null;

	static public MobilityEstimator getInstance(/*
												 * int ieee80211Interface, int
												 * bluetoothInterface
												 */) throws Exception {
		if (mobilityEstimatorInstance == null)
			mobilityEstimatorInstance = new MobilityEstimator(/*
															 * ieee80211Interface,
															 * bluetoothInterface
															 */);
		return mobilityEstimatorInstance;
	}

	/*
	 * public MobilityEstimator()throws Exception{
	 * this(MobilityEstimator.StandardDevice,MobilityEstimator.StandardDevice);
	 * }
	 */
	private MobilityEstimator(/* int ieee80211Interface, int bluetoothInterface */)
			throws Exception {
		// if(ieee80211Interface!=MobilityEstimator.UNUSED){
		try {
			mobilityEstimatorIEEE80211 = new MobilityEstimatorIEEE80211(/* ieee80211Interface */);
			mobilityEstimatorIEEE80211.start();
		} catch (Exception e) {
			// No IEEE 802.11
			System.out.println("MobilityEstimator: no IEEE80211");
		}
		// }
		// if(bluetoothInterface!=MobilityEstimator.UNUSED){
		try {
			mobilityEstimatorBluetooth = new MobilityEstimatorBluetooth(/*
																		 * bluetoothInterface,
																		 */this);
			mobilityEstimatorBluetooth.start();
		} catch (Exception e) {
			// No Bluetooth
			System.out.println("MobilityEstimator: no Bluetooth");
		}
		// }
	}

	public float getMobilityDegree() {
		if (mobilityEstimatorIEEE80211 != null)
			return mobilityEstimatorIEEE80211.getMobilityDegree();
		else
			return -1;
	}

	public int getMobilityState() {
		if (mobilityEstimatorIEEE80211 != null)
			return mobilityEstimatorIEEE80211.getMobilityState();
		else
			return MobilityEstimator.NOT_AVAILABLE;
	}

	/*
	 * public void startMobilityEstimator(){
	 * if(mobilityEstimatorIEEE80211!=null) mobilityEstimatorIEEE80211.start();
	 * if(mobilityEstimatorBluetooth!=null) mobilityEstimatorBluetooth.start();
	 * }
	 */
	public void stopMobilityEstimator() {
		if (mobilityEstimatorIEEE80211 != null)
			mobilityEstimatorIEEE80211.stopMobilityEstimatorIEEE80211();
		if (mobilityEstimatorBluetooth != null)
			mobilityEstimatorBluetooth.stopMobilityEstimatorBluetooth();
		MobilityEstimator.mobilityEstimatorInstance = null;
	}

	public void setPhase(int newPhase) {
		if (currentPhase == MobilityEstimator.CONNECTED
				&& newPhase == MobilityEstimator.SEARCH) {
			// 80211 invariato
			// bluetooth monitora tutte le device
			this.currentPhase = newPhase;
		} else if (currentPhase == MobilityEstimator.SEARCH
				&& newPhase == MobilityEstimator.CONNECTED) {
			// 80211 invariato
			// bluetooth monitora solo device attualmente connesse
			this.currentPhase = newPhase;
		}
	}

	public int getPhase() {
		return this.currentPhase;
	}

	public Vector<LocationOss> getJoint() {
		if (mobilityEstimatorIEEE80211 != null)
			return mobilityEstimatorIEEE80211.getJoint();
		if (mobilityEstimatorBluetooth != null)
			return mobilityEstimatorBluetooth.getJoint();
		else
			return null;
	}
}
